package com.spring.springboot.sys.core.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author yizhousong
 * @date 2019/10/18 17:36
 */
@ApiModel(description = "返回体")
public class Result {

    @ApiModelProperty("返回数据")
    private Object data;

    @ApiModelProperty("返回信息")
    private String message;

    @ApiModelProperty("状态码")
    private int code;

    public Result() {
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    /**
     * 响应数据返回
     *
     * @param message
     * @return
     */
    public static Result success(String message) {
        return buildBase(200, message);
    }

    /**
     * 响应数据返回
     *
     * @param message
     * @param data
     * @return
     */
    public static Result success(String message, Object data) {
        Result result = buildBase(200, message);
        result.setData(data);
        return result;
    }

    /**
     * 响应失败信息返回
     *
     * @param message
     * @return
     */
    public static Result error(String message) {
        return buildBase(500, message);
    }

    /**
     * 响应状态及失败信息返回
     *
     * @param status
     * @param message
     * @return
     */
    public static Result error(int status, String message) {
        return buildBase(status, message);
    }

    /**
     * 初始化响应实体
     *
     * @param code
     * @param message
     * @return
     */
    private static Result buildBase(int code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    @Override
    public String toString() {
        return "Result{" +
                "data=" + data +
                ", message='" + message + '\'' +
                ", code=" + code +
                '}';
    }
}
